package com.wyvoid.forum.dao.hibernate;

import java.util.List;

import com.wyvoid.forum.dao.IForumDAO;
import com.wyvoid.forum.domain.Forum;
import com.wyvoid.forum.Constant;
import com.wyvoid.core.persistent.hibernate.HibernateDAO;

/**
 * 论坛信息Hibernate数据处理实现 
 * 
 * @author wyvoid
 * @link www.wyvoid.com
 * 
 * @version $Revision: 1.11 $ $Date: 2006/07/28 17:04:32 $
 */
public class ForumDAO extends HibernateDAO<Forum> implements IForumDAO {
	
	/**
	 * 查找全部的论坛信息
	 * <p>
	 * 	以树行结构显示整个论坛树
	 * </p>
	 * @return List 论坛集合
	 */
	public List<Forum> getForums() {
        return doQueryList("from Forum as forum order by forum.depth,forum.type,forum.viewOrder asc",true);
	}
	
	/**
	 * 根据父类编号取得论坛列表
	 * @param parentId 父类编号
	 * @return 论坛版块列表
	 */
	public List<Forum> getForums(long parentId) {
        return doQueryList("from Forum as forum where  forum.parentId=? order by forum.viewOrder desc",true, parentId);
	}
	
	/**
	 * 取得子层中最大的排序编号
	 * 
	 * @param parentId 父节点编号
	 * @return int 取得节点的排序编号
	 */
	public long findChildMaxOrder(int parentId){
        return super.doQueryCount("select max(forum.displayOrder) from Forum as forum where forum.parentId='"+parentId+"'");
	}

    public List<Forum> getGroupForums(long groupId) {
        if(groupId==0){
            return doQueryList("from Forum where type=? and parentId=0 and status=?  order by viewOrder desc ",true,Constant.Forum.TYPE_GROUP,Constant.Forum.STATUS_NORMAL);
        }else{
            return doQueryList("from Forum where id=? and status=?",true,groupId,Constant.Forum.STATUS_NORMAL);
        }
    }

    public List<Forum> getOpenedForums() {
        return doQueryList("from Forum as forum where forum.status=? order by forum.depth,forum.type,forum.viewOrder asc",true,Constant.Forum.STATUS_NORMAL);
    }

    public List getValidForums(long parentForumId) {
        return doQueryList("from Forum as forum where forum.status=? and  forum.parentId=? order by forum.viewOrder desc",true,Constant.Forum.STATUS_NORMAL ,parentForumId);
    }
}